package examples;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;

public class MereniCpu {
	public static void CPUprehled() {
		/* Total number of processors or cores available to the JVM */
		System.out.println("Available processors (cores): " + 
		Runtime.getRuntime().availableProcessors());
		
		/* Total amount of free memory available to the JVM */
		System.out.println("Free memory (bytes): " + 
		Runtime.getRuntime().freeMemory());
		
		/* This will return Long.MAX_VALUE if there is no preset limit */
		long maxMemory = Runtime.getRuntime().maxMemory();
		/* Maximum amount of memory the JVM will attempt to use */
		System.out.println("Maximum memory (bytes): " + 
		(maxMemory == Long.MAX_VALUE ? "no limit" : maxMemory));
		
		/* Total memory currently in use by the JVM */
		System.out.println("Total memory (bytes): " + 
		Runtime.getRuntime().totalMemory());
	}
	
    public static long getCpuTime( ) {
	    ThreadMXBean bean = ManagementFactory.getThreadMXBean( );
	    return bean.isCurrentThreadCpuTimeSupported( ) ?
	        bean.getCurrentThreadCpuTime( ) : 0L;
	}
	 
	/** Get user time in nanoseconds. */
	public static long getUserTime( ) {
	    ThreadMXBean bean = ManagementFactory.getThreadMXBean( );
	    return bean.isCurrentThreadCpuTimeSupported( ) ?
	        bean.getCurrentThreadUserTime( ) : 0L;
	}

	/** Get system time in nanoseconds. */
	public static long getSystemTime( ) {
	    ThreadMXBean bean = ManagementFactory.getThreadMXBean( );
	    return bean.isCurrentThreadCpuTimeSupported( ) ?
	        (bean.getCurrentThreadCpuTime() - bean.getCurrentThreadUserTime( )) : 0L;
	}
}
